﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using TimKen.App;
using TimKen.DataEntity;

namespace TimKen.Web
{
    public partial class POption_List : TimKen.App.AdminPage
    {
        private TimKen.BussLogic.PCategoryManager typem = new TimKen.BussLogic.PCategoryManager();

        public string CategoryID = "";
        public override string ActionCode
        {
            get
            {
                return "89";
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            CategoryID = Request.QueryString["CategoryID"] == null ? "" : Request.QueryString["CategoryID"].ToString();
            if (TypeParse.IsNumeric(CategoryID))
            {
                if (!this.IsPostBack)
                {
                    BinddplCategroy();
                    BindGrid();
                }
            }
            else
            {
                Alert(GetLangString("InvalidCategroyID"));
            }


            ScriptManager.GetCurrent(this.Page).RegisterPostBackControl(this.dplCategroy);
        }

        protected void grdOption_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Remove")
            {
                typem.RemoveOptionFromCategory(e.CommandArgument.ToString(), CategoryID);
                BindGrid();
            }
            if (e.CommandName == "UpdateOrder")
            {
                ImageButton lb = (ImageButton)e.CommandSource;
                TextBox t = lb.Parent.Controls[1] as TextBox;
                int order = 50;
                if (!int.TryParse(t.Text, out order) || order < 0)
                {
                    Alert(GetLangString("InValidOrderNumber"));
                }
                typem.UpdateSortOrder(CategoryID, e.CommandArgument.ToString(), order);
                BindGrid();
            }
        }

        private void BindGrid()
        {
            DataSet ds = typem.GetOptionByCategory(AspNetPager1.PageSize, AspNetPager1.CurrentPageIndex, CategoryID, this.LanguageID.ToString());
            grdOption.DataSource = ds.Tables[0].DefaultView;
            grdOption.DataBind();
            AspNetPager1.RecordCount = Convert.ToInt32(ds.Tables[1].Rows[0]["totle"]);
        }

        private void BinddplCategroy()
        {
            TimKen.BussLogic.PCategoryManager cm = new TimKen.BussLogic.PCategoryManager();
            dplCategroy.DataSource = cm.GetAllLeafCategory(LanguageWhereStr);

            dplCategroy.DataTextField = "CategoryName";
            dplCategroy.DataValueField = "CategoryID";
            dplCategroy.DataBind();


            if(CategoryID!="")
            for(int i =0 ;i< dplCategroy.Items.Count;i++)
                 if(dplCategroy.Items[i].Value == CategoryID)
                     dplCategroy.Items[i].Selected = true;
        }

        protected void dplCategroy_SelectedIndexChanged(object sender, EventArgs e)
        {
            CategoryID = dplCategroy.SelectedValue;
            BindGrid();
        }

        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            BindGrid();
        }
    }
}
